package cn.gztang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * @author GaoFeng2017
 * @date 2018-02-23 16:51:35
 **/

@Controller
public class CaptchaController {
    private static final int WIDTH = 90;
    private static final int HEIGHT = 34;

    /*随机输出验证码*/
    @RequestMapping("/captcha")
    public void createCaptcha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        //填充背景
        setBackground(g);
        //绘制随机线条
        setLine(g);

        //绘制验证码内容
        String code = setCode((Graphics2D)g);
        request.getSession().setAttribute("checkCode", code);
        OutputStream os = response.getOutputStream();
        response.setContentType("image/jpeg");
        response.setHeader("Expires", "-1");
        response.setHeader("Cache-Control", "no-cache");
        ImageIO.write(image, "jpg", os);
        os.close();
    }


    private void setBackground(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
    }

    private void setLine(Graphics g) {
        g.setColor(Color.GREEN);

        Random rd = new Random();
        for(int i = 1;i <= 6;i++) {
            int x1 = rd.nextInt(WIDTH)-2;
            int y1 = rd.nextInt(HEIGHT)-2;
            int x2 = rd.nextInt(WIDTH)-2;
            int y2 = rd.nextInt(HEIGHT)-2;
            g.drawLine(x1, y1, x2, y2);
        }
    }
    private Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
        if (fc > 255) {
            fc = 255;
        }

        if (bc > 255) {
            bc = 255;
        }
        Random random = new Random();
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    private Font getFont(int size) {
        Random random = new Random();
        Font font[] = new Font[4];
        font[0] = new Font("Ravie", Font.PLAIN, size);
        font[1] = new Font("Antique Olive Compact", Font.PLAIN, size);
        font[2] = new Font("Fixedsys", Font.PLAIN, size);
        font[3] = new Font("微软雅黑", Font.PLAIN, size);
        return font[random.nextInt(4)];
    }
    private String setCode(Graphics2D g) {

        String base = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random rd = new Random();
        int x = 7;
        StringBuilder sb = new StringBuilder();
        g.setFont(getFont(24));
        for(int i = 1;i <= 4;i++) {
            String str = base.charAt(rd.nextInt(base.length()))+"";
            sb.append(str);
            int degree = rd.nextInt(30);
            g.rotate(degree*Math.PI/180,x,20);
            g.setColor(this.getRandColor(60,180));
            g.drawString(str, x, 20);
            g.rotate(-degree*Math.PI/180,x,20);
            x += 18;
        }

        return new String(sb);
    }
}
